package com.itheima.stock.handler;

import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.StockException;
import com.itheima.stock.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: zyx
 * @created: 2025-11-07-09:01
 * @description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 统一处理 业务异常
     *
     * @param e StockException
     * @return 统一响应结果
     */
    @ExceptionHandler(StockException.class)
    public ResponseResult<?> handleStockException(StockException e){
        return ResponseResult.error(e.getCode());
    }


    @SuppressWarnings("all")
    @ExceptionHandler(Exception.class)
    public ResponseResult<?> handleUnknownException(Exception e){
        log.error("系统异常", e);
        e.printStackTrace();
        return ResponseResult.error(CodeEnum.SYSTEM_ERROR);
    }
}
